package com.linked.pay.coupon.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.pay.coupon.bean.param.CouponInfoListParam;
import com.linked.pay.coupon.bean.param.UserGetCouponParam;
import com.linked.pay.coupon.bean.po.CouponInfoPO;
import com.linked.pay.coupon.bean.vo.UserCouponListVO;
import com.linked.pay.coupon.service.ICouponHandleService;
import com.linked.universal.common.LinkedResult;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :dbq
 * @date : 2023/3/6 9:32
 * @description : desc
 */
@Slf4j
@RestController
@RequestMapping("couponhandle")
public class CouponHandleController {

    private final ObjectMapper mapper;

    private final ICouponHandleService couponHandleService;

    private final RedisTemplate redisTemplate;

    @Autowired
    public CouponHandleController(ObjectMapper mapper, ICouponHandleService couponHandleService, RedisTemplate redisTemplate) {
        this.mapper = mapper;
        this.couponHandleService = couponHandleService;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 查询用户优惠券列表
     */
    @PostMapping("queryUserCouponList")
    Result queryUserCouponList(@RequestBody CouponInfoListParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("查询用户优惠券列表接口 入参：", mapper.writeValueAsString(param));
        }

        List<UserCouponListVO> res = null;
        try {
            res = couponHandleService.queryUserCouponList(param);
        } catch (Exception e) {
            log.error("查询用户优惠券列表接口 异常", e);
            return Result.error("程序异常，请联系管理员！");
        }
        return CollectionUtils.isEmpty(res) ? Result.ok(false, "未查询到数据！") : Result.success(res);
    }

    /**
     * 查询优惠券种类列表
     */
    @PostMapping("queryCouponInfoList")
    Result queryCouponInfoList() throws Exception {
        if (log.isInfoEnabled()) {
            log.info("查询优惠券种类列表接口 入参：", mapper.writeValueAsString(""));
        }

        List<CouponInfoPO> res = null;
        try {
            res = couponHandleService.queryCouponInfoList();
        } catch (Exception e) {
            log.error("查询优惠券种类列表接口 异常", e);
            return Result.error("程序异常，请联系管理员！");
        }
        return CollectionUtils.isEmpty(res) ? Result.ok(false, "未查询到数据！") : Result.success(res);
    }

    /**
     * 增加优惠券信息接口
     */
    @PostMapping("addCouponInfo")
    Result addCouponInfo(@RequestBody CouponInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("增加优惠券信息接口 入参：", mapper.writeValueAsString(param));
        }

        boolean ret = false;
        try {
            ret = couponHandleService.addCouponInfo(param);
        } catch (Exception e) {
            log.error("增加优惠券信息接口 异常", e);
            return Result.error("程序异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "保存失败！");
    }

    /**
     * 领取优惠券
     */
    @PostMapping("userGetCoupon")
    LinkedResult userGetCoupon(@RequestBody UserGetCouponParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("领取优惠劵接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = couponHandleService.userGetCoupon(param);
        } catch (Exception e) {
            log.error("领取优惠劵接口 异常", e);
            return LinkedResult.Error("程序异常，请联系管理员！");
        }
        return ret ? LinkedResult.Success() : LinkedResult.OK(false, "领取失败！");
    }

    /**
     * 抢券
     */
    @PostMapping("grabCoupon")
    LinkedResult grabCoupon(@RequestBody UserGetCouponParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("抢券接口 入参：{}", mapper.writeValueAsString(param));
        }


        return LinkedResult.Success();
    }
}
